package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author 山顶洞人郑某
 * @className DishController.java
 * @date 2024 01 20
 */
@Api("菜品相关接口")
@RestController
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 添加菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("添加菜品接口")
    @PostMapping
    public Result<String> save(@RequestBody DishDTO dishDTO){
         redisTemplate.delete("dish_"+dishDTO.getCategoryId());
         log.info("新增菜品为{}",dishDTO);
         dishService.save(dishDTO);
         return Result.success("添加成功");
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询信息{}",dishPageQueryDTO);
       PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 菜品删除与批量删除
     * @return
     */
    @DeleteMapping
    @ApiModelProperty(value = "批量删除菜品")
    public Result<String > deleteByIds(@RequestParam List<Long> ids){ //该注解用与动态解析传入的字符串并赋值给list集合
        //在批量删除菜品后，redis缓存的数据要进行删除更新
        Set keys = redisTemplate.keys("dish_*");//先获取所有的keys
        if (keys != null) {
            redisTemplate.delete(keys);//再将key删除
        }
        log.info("删除菜品id{}",ids);
        dishService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 根据id查询菜品信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询菜品信息")
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id){
        DishVO dishVO =  dishService.getById(id);
        log.info("查询的菜品信息为{}",dishVO);
       return Result.success(dishVO);
    }

    /**
     * 菜品信息的修改
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改菜品信息")
    public Result<String > updateById(@RequestBody DishDTO dishDTO){
        redisTemplate.delete("dish_id"+dishDTO.getCategoryId());
        dishService.updateWithFlavor(dishDTO);
        return Result.success();
    }

    /**
     * 菜品状态修改
     * @param status
     * @param id
     * @return
     */

    @ApiOperation(value = "菜品状态修改")
    @PostMapping("/status/{status}")
    public Result<String> updateStatus(@PathVariable Integer status,Long id){
        Set keys = redisTemplate.keys("dish_*");
        if (keys != null) {
            redisTemplate.delete(keys);
        }
        dishService.updateStatus(status,id);
       return Result.success();
    }

    /**
     * 根据菜品分类查询菜品
     * @param categoryId
     * @return
     */

    @ApiOperation("根据菜品分类查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> list(Long categoryId ){
       List<Dish> list =  dishService.list(categoryId);
       return Result.success(list);
    }
}
